﻿using System;
using System.Collections.Generic;
using System.Text;
using AMStudio.Script;

namespace AMStudio.Common
{
    [Serializable]
    public class RuntimeItem
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string dataType;

        public string DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }
        private int length;

        public int Length
        {
            get { return length; }
            set { length = value; }
        }
        private string tagID;

        public string TagID
        {
            get { return tagID; }
            set { tagID = value; }
        }
        private int position;

        public int Position
        {
            get { return position; }
            set { position = value; }
        }
        private string module;

        public string Module
        {
            get { return module; }
            set { module = value; }
        }
        private string relateTag;

        public string RelateTag
        {
            get { return relateTag; }
            set { relateTag = value; }
        }
        private string deviceTag;

        public string DeviceTag
        {
            get { return deviceTag; }
            set { deviceTag = value; }
        }
        private string action;

        public string Action
        {
            get { return action; }
            set { action = value; }
        }
        private string comments;

        public string Comments
        {
            get { return comments; }
            set { comments = value; }
        }
        public virtual void SetData(object value)
        {
        }
        public virtual object GetData()
        {
            return null;
        }
        public virtual void HandleValueChange(System.Action<object> valueChangeHandler)
        {
        }
        public virtual void HandleValueChange(System.Action<object, object> valueChangeHandler)
        {
        }
        public virtual void HandleValueChange(System.Action<object, object> valueChangeHandler, bool valueChangeDetect)
        {
        }
    }
    public class RuntimeTag<T> : RuntimeItem, IRefrenceValueTag<T>
    {
        private IRefrenceTagProvider m_TagProvider;
        public IRefrenceTagProvider TagProvider
        {
            get
            {
                return m_TagProvider;
            }
            set
            {
                m_TagProvider = value;
            }
        }
        public T m_Value;
        public T Value
        {
            get
            {
                m_TagProvider.ReadTag(this);
                return m_Value;
            }
            set
            {
                this.m_Value = value;
                m_TagProvider.WriteTag(this);
            }
        }
        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            if (this.Value is Array)
            {
                Array array = this.Value as Array;
                for (int i = 0; i < array.Length; i++)
                {
                    str.Append(array.GetValue(i));
                }
            }
            else
            {
                str.Append(this.Value);
            }
            return str.ToString();
            //return this.m_TagProvider.GetTagString(this);
        }
        public override void SetData(object value)
        {
            try
            {
                m_Value = (T)value;
            }
            catch (Exception ex)
            {
                //Logging.WriteException(ex);
                //Logging.WriteWarning("set data error" + this.Name);
            }
        }
        public override object GetData()
        {
            return m_Value;
        }
        public void SetValueByIndex(int index, object value)
        {
            m_TagProvider.WriteTagByIndex(this, index, value);
        }

        private T oldValue = default(T);
        public override void HandleValueChange(System.Action<object> valueChangeHandler)
        {
            if (this.Value is Array)
            {
                StringBuilder currentStr = new StringBuilder();
                StringBuilder oldStr = new StringBuilder();
                Array currentArray = this.Value as Array;
                for (int i = 0; i < currentArray.Length; i++)
                {
                    currentStr.Append(currentArray.GetValue(i));
                }
                Array oldArray = this.oldValue as Array;
                if (oldArray != null)
                {
                    for (int i = 0; i < oldArray.Length; i++)
                    {
                        oldStr.Append(oldArray.GetValue(i));
                    }
                }
                if (currentStr.ToString() != oldStr.ToString())
                {
                    valueChangeHandler(this);
                    this.oldValue = (T)currentArray.Clone();
                }
            }
            else
            {
                //currentStr.Append(this.Value);
                //oldStr.Append(this.oldValue);
                if (Value.ToString() != oldValue.ToString())
                {
                    valueChangeHandler(this);
                    this.oldValue = Value;
                }
            }
        }
        public override void HandleValueChange(System.Action<object, object> valueChangeHandler)
        {
            if (this.Value is Array)
            {
                StringBuilder currentStr = new StringBuilder();
                StringBuilder oldStr = new StringBuilder();
                Array currentArray = this.Value as Array;
                for (int i = 0; i < currentArray.Length; i++)
                {
                    currentStr.Append(currentArray.GetValue(i));
                }
                Array oldArray = this.oldValue as Array;
                if (oldArray != null)
                {
                    for (int i = 0; i < oldArray.Length; i++)
                    {
                        oldStr.Append(oldArray.GetValue(i));
                    }
                }
                if (currentStr.ToString() != oldStr.ToString())
                {
                    valueChangeHandler(this.Value, this);
                    this.oldValue = (T)currentArray.Clone();
                }
            }
            else
            {
                //currentStr.Append(this.Value);
                //oldStr.Append(this.oldValue);
                if (Value.ToString() != oldValue.ToString())
                {
                    valueChangeHandler(this.Value, this);
                    this.oldValue = Value;
                }
            }
        }
        public override void HandleValueChange(System.Action<object, object> valueChangeHandler, bool valueChangDetect)
        {
            if (valueChangDetect)
            {
                HandleValueChange(valueChangeHandler);
            }
            else
            {
                valueChangeHandler(this.Value, this);
                if (this.Value is Array)
                {
                    Array currentArray = this.Value as Array;
                    this.oldValue = (T)currentArray.Clone();
                }
                else
                {
                    this.oldValue = Value;
                }
            }
        }

    }

    [Serializable]
    public class MachineInformationItem
    {
        private int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }
        private string code;

        public string Code
        {
            get { return code; }
            set { code = value; }
        }
        private string address;

        public string Address
        {
            get { return address; }
            set { address = value; }
        }
        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }
        private string station;

        public string Station
        {
            get { return station; }
            set { station = value; }
        }
        private int priority;

        public int Priority
        {
            get { return priority; }
            set { priority = value; }
        }
        private string hardware;

        public string Hardware
        {
            get { return hardware; }
            set { hardware = value; }
        }
        private string comments;

        public string Comments
        {
            get { return comments; }
            set { comments = value; }
        }



        public bool Value = false;
     
    }

    public class MachineInformationItemStatistics
    {
        private DateTime starttime;
        public DateTime StartTime
        {
            get { return starttime; }
            set { starttime = value; }
        }

        private DateTime endtime;
        public DateTime EndTime
        {
            get { return endtime; }
            set { endtime = value; }
        }

        private TimeSpan durationtime;
        public TimeSpan DurationTime
        {
            get { return durationtime; }
            set { durationtime = value; }
        }

        private int nestID;
        public int NestID
        {
            get { return nestID; }
            set { nestID = value; }
        }

        private MachineInformationItem item;
        public MachineInformationItem Item
        {
            get { return item; }
            set { item = value; }
        }
    }
}
